package future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 并发工具  CompletableFuture
 */
public class TestCompletableFuture {

    public static void main(String[] args) {
        CompletableFuture<String> cf = CompletableFuture.completedFuture("message");
        assert cf.isDone();
        System.out.println(cf.getNow(null));


        ExecutorService service = Executors.newCachedThreadPool();
        CompletableFuture<Object> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("异步执行");
            return "ok-supplyAsync";
        }, service).thenApply(x -> {
            System.out.println("参数：" + x);
            return "ok-thenApply";
        });
        completableFuture.thenAccept(s -> {
            System.out.println("接收上一个参数：" + s);
        });
        if (completableFuture.isDone()){
            service.shutdown();
        }
    }
}
